﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Base.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.SqlExport
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Base class for a data export
    /// </summary>
    public abstract class Base : Task.Base<RecordProcessor>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Base"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// Name of the task
        /// </param>
        /// <param name="connectionString">
        /// Database connection string
        /// </param>
        /// <param name="sqlCommand">
        /// The SQL command to execute
        /// </param>
        /// <param name="parameters">
        /// Parameters for the SQL command
        /// </param>
        protected Base(string taskName, string connectionString, string sqlCommand, SqlParameter[] parameters)
            : base(taskName)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }

            if (string.IsNullOrWhiteSpace(sqlCommand))
            {
                throw new ArgumentNullException("sqlCommand");
            }

            this.ConnectionString = connectionString;
            this.SqlCommand = sqlCommand;
            this.Parameters = parameters;

            this.DbHelper = new MSSql10();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the Database helper class
        /// </summary>
        protected MSSql10 DbHelper { get; private set; }

        /// <summary>
        /// Gets or sets ConnectionString.
        /// </summary>
        private string ConnectionString { get; set; }

        /// <summary>
        /// Gets or sets Parameters.
        /// </summary>
        private SqlParameter[] Parameters { get; set; }

        /// <summary>
        /// Gets or sets SqlCommand.
        /// </summary>
        private string SqlCommand { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Event for outputing the results
        /// </summary>
        /// <param name="dataReader">
        /// The results to output
        /// </param>
        /// <returns>
        /// The on output results.
        /// </returns>
        protected abstract long OnOutputResults(IDataReader dataReader);

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// object that contains result information
        /// </param>
        protected override void OnProcess(RecordProcessor result)
        {
            result.RecordsProcessed = this.DbHelper.ActUponDataReader(
                this.ConnectionString, this.SqlCommand, this.Parameters, this.OnOutputResults, false);
        }

        #endregion
    }
}